package com.demo.mb;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.jfinal.aop.Before;
import com.jfinal.core.BaseController;
import com.jfinal.core.Const;
import com.jfinal.core.Controller;
import com.jfinal.ext.interceptor.SearchSql;
import com.jfinal.kit.JsonKit;
import com.jfinal.kit.StringKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.ehcache.CacheKit;
import com.jfinal.render.JsonRender;
import com.demo.util.Constants;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MbController extends BaseController {

	public void index() {
		//render("/WEB-INF/view/mb/cy.html");

		render("/WEB-INF/view/mb/index.html");
	}
	
	public void getview() {
		String id=getPara("id");
		List<Record> mblist=Db.find("select * from wx_mb where id=?",id);
		List<Record> mbsxlist=Db.find("select * from wx_mbsx where type='img'  and lbid=?",id);
		setAttr("mbsxlist",mbsxlist);
		setAttr("mblist",mblist);
		render("/WEB-INF/view/mb/view.html");

//	render("/WEB-INF/view/mb/index.html");
}

	public void add() {
		String lbid = getPara("id");
		String name = getPara("name");
		String pid = getPara("pid");
		setAttr("lbid", lbid);
		setAttr("name", name);
		setAttr("pid", pid);
		render("/WEB-INF/view/mb/add.html");
	}


    public void addlb() {
		String lbid = getPara("lbid");
		String name = getPara("name");
		String pid = getPara("pid");
		setAttr("lbid", lbid);
		setAttr("name", name);
		setAttr("pid", pid);
		String id = getPara("id");
		String where = "";
		List<Record> list = null;
		if (StringKit.notBlank(id)) {
			where += " where id =" + id + "  ";
			list = WxMb.dao.findtree(where);
		} else {
			list = WxMb.dao.findtree("where id= null");
		}
		setAttr("list", list);
		render("/WEB-INF/view/mb/add_lb.html");
	}

    public void addwp() {
		String lbid = getPara("lbid");
		String name = getPara("name");
		String pid = getPara("pid");
		String id = getPara("id");
		List<Record> pic = null;
		List<Record> newlist = Db.find("select  * from wx_mbsx     where    id=  '" + id + "'");
	
		setAttr("lbid", lbid);
		setAttr("name", name);
		setAttr("pid", pid);
		setAttr("list", newlist);
		setAttr("pic", pic);

		render("/WEB-INF/view/mb/add_wp.html");
	}



	public void addcy() {
		String lbid = getPara("lbid");
		
		List<Record> newlist = Db.find("select  * from wx_mb    where    id=  '" + lbid + "'");
		List<Record> sxlist = Db.find("select  * from wx_mbsx    where   type='img'  and  lbid=  '" + lbid + "' ");

		setAttr("lbid", lbid);
		setAttr("list", newlist);
		setAttr("sxlist", sxlist);
		render("/WEB-INF/view/mb/cy.html");
		}

	
	public void getWpTree() {
		String id = getPara("pid");
		String yxbz = getPara("yxbz");
		String where = "";
		if (StringKit.notBlank(yxbz)) {
			where += " where yxbz =" + yxbz + "  ";
		}
		List<Record> newlist = WxMb.dao.findtree(where);
		JSONObject jo1 = new JSONObject();
		// / 增加根节点
		jo1.put("id", "0");
		jo1.put("pId", "0");
		jo1.put("name", "无");
		JSONArray jsonArr = new JSONArray();
		if (newlist.size() == 0) {
			jsonArr.add(jo1);
			renderJson(jsonArr);
		//	render(jsonArr);

		} else {
			for (int j = 0; j < newlist.size(); j++) {
				JSONObject jo = new JSONObject();
				jo.put("id", newlist.get(j).get("id"));
				jo.put("pId", newlist.get(j).get("pId"));
				jo.put("name", newlist.get(j).get("name"));
				jsonArr.add(jo);
			}
			jsonArr.add(jo1);
		}
		renderJson(jsonArr);
	//	render(jsonArr);

	}

	/**
	 * 在增加了SearchSql拦截之后，就可以根据页面的配置信息
	 * 自动装配查询语句，并放入request中，可以通过getAttr(Const.SEARCH_SQL)函数获取到 根据实际需要自动组装到sql语句中
	 */
	@Before(SearchSql.class)
	public void query() {
		int pageNumber = getParaToInt("page");
		int pageSize = getParaToInt("rows");
		String where = getAttr(Const.SEARCH_SQL);
		String search = getPara("search_EQ_lbid");
		System.out.println(search);

//		if (StringKit.notBlank(search)) {
//			if (search.equals("0")) {
//				where = "";
//			}
//		}
		Page<WxMbsx> page = WxMbsx.dao.page(pageNumber, pageSize, where);
		Map root = new HashMap();
		// / jqgrid用 分页显示
		root.put("total", page.getTotalPage());
		root.put("page", page.getPageNumber());
		  root.put("records", page.getTotalRow());
	        root.put("rows", page.getList());
		renderJson(JsonKit.toJson(root));
		//	render(JsonKit.toJson(root));

	}

	/**
	 * 模板保存
	 */
	public void savelb() {
		String mbmc = getPara("mbmc");
		List<Record> max = Db.find("Select count(*) as max  from wx_mb where yxbz='1'  and mbmc='"
						+ mbmc + "'");
		String maxs = String.valueOf(max.get(0).get("max"));
		
		if (!maxs.equals("0")) {
		//	renderJson("存在此模板！");
			renderText("存在此模板！");

		} else {
			if (getModel(WxMb.class).save()) {
				List<Record>  mblist = Db.find("Select  *   from wx_mb where  yxbz='1'    order by dsporder");
				System.out.println("==========="+mblist);
				CacheKit.removeAll("mblist");
				CacheKit.put("mblist", "mblist", mblist);
				renderText(Constants.SAVESUCCESS);
			} else {
				renderText(Constants.SAVEFAILED);
			}
		}

	}


	
    
	/**
	 * 步骤保存
	 */
	public void savewp() {
		String mbbh = getPara("mbbh");
		String lbid=getPara("lbid");
		String mbsxmc = getPara("mbsxmc");
		// 类别名称物品名称不重复
//		List<Record> max = Db
//				.find("Select count(*) as max  from wx_mbsx where lbid=='"+lbid+"'  and (   mbbh='"
//						+ mbbh + "' or mbsxmc= '" + mbsxmc + "'  )  ");
//		String maxs = String.valueOf(max.get(0).get("max"));
//		if (!maxs.equals("0")) {
//			renderJson("存在此步骤！");
//		} else {
			if (getModel(WxMbsx.class).save()) {
				CacheKit.removeAll("mbtypelist");

				renderText(Constants.SAVESUCCESS);
			} else {
				renderText(Constants.SAVEFAILED);
			}
//		}

	}
    
	/**
	 * 步骤保存
	 */
	public void savembsx() {
		WxMbsx mbsx = getModel(WxMbsx.class);

		// 类别名称物品名称不重复
//		List<Record> max = Db
//				.find("Select count(*) as max  from wx_mbsx where lbid=='"+lbid+"'  and (   mbbh='"
//						+ mbbh + "' or mbsxmc= '" + mbsxmc + "'  )  ");
//		String maxs = String.valueOf(max.get(0).get("max"));
//		if (!maxs.equals("0")) {
//			renderJson("存在此步骤！");
//		} else {
			if (mbsx.save()) {
				System.out.println("mbsx.get().toString()===================="+mbsx.get("id").toString());
				renderText(mbsx.get("id").toString());
			} else {
				renderText("0");
			}
//		}

	}
	

	/**
	 * 步骤保存
	 */
	public void updatembsx() {
		String id=getPara("id");
		String nr=getPara("nr");
		String url=getPara("url");
		String color=getPara("color");
		String tx=getPara("tx");//特效
		String yc=getPara("yc");//延迟
		Record xx=new Record();
		xx.set("id", id).set("nr",nr).set("url", url)
		.set("color",color).set("tx",tx).set("yc",yc);
		boolean flag=Db.update("wx_mbsx",xx);

			if (flag) {
				renderText(id);
			} else {
				renderText("0");
			}
//		}

	}

	/**
	 * 类别修改
	 */
	public void updatelb() {
		String mbmc = getPara("mbmc");
		String id = getPara("id");

		List<Record> max = Db
				.find("Select count(*) as max  from wx_mb where yxbz='1' and mbmc='"
						+ mbmc + "'  and id != '" + id + "'");
	
		String maxs = String.valueOf(max.get(0).get("max"));
		if (!maxs.equals("0")) {
		//	renderJson("存在此模板！");
			renderText("存在此模板！");

		} else {
			if (getModel(WxMb.class).update()) {
				List<Record>  mblist = Db.find("Select *  from wx_mb   where yxbz='1' order by dsporder");
				System.out.println("==========="+mblist);
				CacheKit.removeAll("mblist");
				CacheKit.put("mblist", "mblist", mblist);
				renderText(Constants.EDITSUCCESS);
			} else {
				renderText(Constants.EDITFAILED);
			}
		}

	}

	/**
	 * 步骤修改
	 */
	public void updatewp() {
		String mbbh = getPara("mbbh");
		String mbsxmc = getPara("mbsxmc");
		String id = getPara("id");

		// 类别名称物品名称不重复
//		List<Record> max = Db
//				.find("Select count(*) as max  from wx_mbsx where mbbh='"
//						+ mbbh + "' and id!='" + id + "'");
//		String maxs = String.valueOf(max.get(0).get("max"));
//		if (!maxs.equals("0")) {
//			renderJson("存在此步骤！");
//		} else {
			if (getModel(WxMbsx.class).update()) {
				CacheKit.removeAll("mbtypelist");

				renderText(Constants.EDITSUCCESS);
			} else {
				renderText(Constants.EDITFAILED);
			}
//		}
	}
	
	public void getmb(){
		String id=getPara("id");
		List<Record> list=Db.find(" select * from  wx_mbsx where id=? ",id);
		renderJson(list);
	}

	
	public void delete() {
		if (WxMb.dao.deleteById(getParaToInt())) {
			renderText(Constants.DELETESUCCESS);
		} else {
			renderText(Constants.DELETEFAILED);
		}
	}

	public void deletembsx() {
		if (getModel(WxMbsx.class).deleteById(getPara("id"))) {
			renderText("1");
		} else {
			renderText("0");
		}
	}

	
}
